/********************************************************************************
 * Project Name		[JavaSE_Fundamentals]
 * File Name     	[ValuePassing.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package objectOriented;

/**
 * Java值传递
 * 
 * 一个方法不能修改一个基本数据类型的参数(数值型和boolean型)<br/>
 * 一个方法不能实现让对象参数引用一个新的对象<br/>
 * 一个方法可以改变一个对象属性的状态(值)<br/>
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class ValuePassing {

	/**
	 * 基本类型值传递
	 * 
	 * I.numberParam被初始化为number的一个拷贝<br>
	 * II.numberParam*3后是30, 而number仍然是10<br>
	 * III.方法结束后, numberParam不再使用<br>
	 */
	public int tripleNumber(int numberParam) {
		return 3 * numberParam;
	}

	/**
	 * 对象引用值传递
	 */
	public void swapPoint(Point pointParam1, Point pointParam2) {
		Point temp = pointParam1;
		pointParam1 = pointParam2;
		pointParam2 = temp;
	}

	/**
	 * 对象属性值传递
	 * 
	 * I.pointParam被初始化为point的拷贝, 这里是一个对象引用.<br>
	 * II.swapCoordinate方法应用与这个对象引用, pointParam和point引用了相同的对象.<br>
	 * III.当方法结束后, pointParam不再使用, 而对象内部的属性值被修改了(同一个对象).<br>
	 */
	public void swapCoordinate(Point pointParam) {
		int temp = pointParam.getX();
		pointParam.setX(pointParam.getY());
		pointParam.setY(temp);
	}

	public static void main(String[] args) {
		ValuePassing methodParameter = new ValuePassing();
		// 基本类型值传递
		int number = 10;
		System.out.println("值传递前: " + number);
		methodParameter.tripleNumber(number);
		System.out.println("值传递后: " + number);

		// 对象引用值传递
		Point point1 = new Point(3, 4);
		Point point2 = new Point(9, 16);
		System.out.println("交换前: point1坐标---" + point1 + "\tpoint2坐标---" + point2);
		methodParameter.swapPoint(point1, point2);
		System.out.println("交换后: point1坐标---" + point1 + "\tpoint2坐标---" + point2);

		// 对象属性值传递
		Point point = new Point(3, 4);
		System.out.println("交换前: point坐标---" + point);
		methodParameter.swapCoordinate(point);
		System.out.println("交换后: point坐标---" + point);
	}
}

/**
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 */
class Point {
	private int x;
	private int y;

	public Point(int x, int y) {
		this.x = x;
		this.y = y;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	@Override
	public String toString() {
		return "(" + x + "," + y + ")";
	}
}